package com.songtech.stateuser.service.impl;

import com.songtech.product.bean.Product;
import com.songtech.product.service.ProductService;
import com.songtech.stateuser.bean.UserStatus;
import com.songtech.stateuser.dao.UserStatusMapper;
import com.songtech.stateuser.service.UserStatusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserStatusServiceImpl implements UserStatusService {
    Logger logger = LoggerFactory.getLogger(UserStatusServiceImpl.class);
    @Resource
    private ProductService productService;

    @Resource
    private UserStatusMapper userStatusMapper;

    /**
     * 记录用户上线或下线
     * @param imei 设备号
     * @param status 状态0上线 ，1下线
     * @param userId 用户id
     */
    @Override
    public void recordUser(String imei,Integer status,Long userId) {
        try{
            Product product = productService.getProductByImei(imei);
            UserStatus userStatus = new UserStatus();
            if(product != null){
                userStatus.setCompanyCode(product.getCompanyCode());
                userStatus.setProductCode(product.getProductCode());
                userStatus.setModelNumber(product.getModelNumber());
            }
            userStatus.setUserId(userId);
            userStatus.setStatus(status);
            userStatus.setImei(imei);
            userStatus.setCreateTime(new Date());
            userStatusMapper.insert(userStatus);
        }catch (Exception e){
            logger.error("同步用户上下线异常: "+e.getMessage(),e);
        }
    }

    @Override
    public int onlineUserCount(Map<String, Object> map) {
        return userStatusMapper.onlineUserCount(map);
    }

    @Override
    public int offlineUserCount(Map<String, Object> map) {
        return userStatusMapper.offlineUserCount(map);
    }

    @Override
    public List<UserStatus> onlineUserByDay(Map<String, Object> map) {
        return userStatusMapper.onlineUserByDay(map);
    }
}
